﻿using DAL;
using DAL.DBManagers;
using DAL.Models;
using System;
using System.Linq;

public partial class ClientEdit : System.Web.UI.Page
{
    Guid RecordID = Guid.Empty;
    Guid ClientsID = Guid.Empty;
    ClientsManager manager = new ClientsManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = String.Empty;

        if (Guid.TryParse(Request.QueryString["id"], out RecordID))
        {
            if (!IsPostBack)
            {
                try
                {
                    if (!IsPostBack)
                    {
                        var client = manager.Single(s => s.RecordID == RecordID);

                        txtName.Text = client.Name;
                        txtNameGeo.Text = client.NameGeo;
                        txtOrisNumber.Text = client.Oris_ACC_Num;
                        txtCN.Text = client.CN;
                        txtAssociationNo.Text = client.Association_No;
                        txtDefaultLanguage.Text = client.Default_Language;
                        txtCountry.Text = client.Country;
                        txtCountryGeo.Text = client.CountryGeo;
                        txtAddress.Text = client.Address;
                        txtAddressGeo.Text = client.AddressGeo;
                        txtPostalCode.Text = client.Postal_Code;
                        txtCity.Text = client.City;
                        txtCityGeo.Text = client.City_Geo;
                        txtTaxRegime.Text = client.TaxRegime;
                        txtTRN.Text = client.TRN;
                        txtOwnerName.Text = client.OwnerName;
                        txtOwnerNameGeo.Text = client.OwnerNameGeo;
                        txtOwnerJobTitle.Text = client.OwnerJobTitle;
                        txtOwnerJobTitleGeo.Text = client.OwnerJobTitleGeo;
                        txtMobile.Text = client.Mobile;
                        txtPhone.Text = client.Phone;
                        txtFax.Text = client.Fax;
                        txtEmail.Text = client.Email1;
                        txtBeginDate.DateValue = client.BeginDate;
                    }
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            var CurrentUser = Session["User"] as UserHelper;

            Guid NewClientID = Guid.Empty;

            if (RecordID == Guid.Empty)
            {
                if (Guid.TryParse(Request.QueryString["sid"], out ClientsID))
                {
                    if (manager.Find(f => f.IsActive && f.ClientID == ClientsID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                    {
                        throw new Exception("Client already exists with this date");
                    }

                    manager.Add(new Client
                    {
                        RecordID = Guid.NewGuid(),
                        ClientID = ClientsID,
                        Name = txtName.Text,
                        NameGeo = txtNameGeo.Text,
                        Oris_ACC_Num = txtOrisNumber.Text,
                        CN = txtCN.Text,
                        Association_No = txtAssociationNo.Text,
                        Default_Language = txtDefaultLanguage.Text,
                        Country = txtCountry.Text,
                        CountryGeo = txtCountryGeo.Text,
                        Address = txtAddress.Text,
                        AddressGeo = txtAddressGeo.Text,
                        Postal_Code = txtPostalCode.Text,
                        City = txtCity.Text,
                        City_Geo = txtCityGeo.Text,
                        TaxRegime = txtTaxRegime.Text,
                        TRN = txtTRN.Text,
                        OwnerName = txtOwnerName.Text,
                        OwnerNameGeo = txtOwnerNameGeo.Text,
                        OwnerJobTitle = txtOwnerJobTitle.Text,
                        OwnerJobTitleGeo = txtOwnerJobTitleGeo.Text,
                        Mobile = txtMobile.Text,
                        Phone = txtPhone.Text,
                        Fax = txtFax.Text,
                        Email1 = txtEmail.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    });

                    NewClientID = ClientsID;
                }
                else
                {
                    var client = new Client
                    {
                        RecordID = Guid.NewGuid(),
                        ClientID = Guid.NewGuid(),
                        Name = txtName.Text,
                        NameGeo = txtNameGeo.Text,
                        Oris_ACC_Num = txtOrisNumber.Text,
                        CN = txtCN.Text,
                        Association_No = txtAssociationNo.Text,
                        Default_Language = txtDefaultLanguage.Text,
                        Country = txtCountry.Text,
                        CountryGeo = txtCountryGeo.Text,
                        Address = txtAddress.Text,
                        AddressGeo = txtAddressGeo.Text,
                        Postal_Code = txtPostalCode.Text,
                        City = txtCity.Text,
                        City_Geo = txtCityGeo.Text,
                        TaxRegime = txtTaxRegime.Text,
                        TRN = txtTRN.Text,
                        OwnerName = txtOwnerName.Text,
                        OwnerNameGeo = txtOwnerNameGeo.Text,
                        OwnerJobTitle = txtOwnerJobTitle.Text,
                        OwnerJobTitleGeo = txtOwnerJobTitleGeo.Text,
                        Mobile = txtMobile.Text,
                        Phone = txtPhone.Text,
                        Fax = txtFax.Text,
                        Email1 = txtEmail.Text,
                        BeginDate = txtBeginDate.DateValue.Value,
                        IsActive = true,
                        CreateDate = DateTime.Now,
                        CreatedBy = CurrentUser.UserID
                    };

                    manager.Add(client);

                    NewClientID = client.ClientID;
                }
            }
            else
            {
                var client = manager.Single(s => s.RecordID == RecordID);

                if (manager.Find(f => f.IsActive && f.ClientID == client.ClientID && f.RecordID != RecordID && f.BeginDate == txtBeginDate.DateValue.Value).Any())
                {
                    throw new Exception("Client already exists with this date");
                }

                client.Name = txtName.Text;
                client.NameGeo = txtNameGeo.Text;
                client.BeginDate = txtBeginDate.DateValue.Value;
                client.LastModifyDate = DateTime.Now;
                client.LastModifier = CurrentUser.UserID;

                NewClientID = client.ClientID;
            }

            manager.SaveChanges();
            manager.RepiarDates(NewClientID);

            if (ClientsID == Guid.Empty && RecordID == Guid.Empty)
            {
                Response.Redirect("clientview.aspx?m=liClient");
            }
            else
            {
                Response.Redirect(String.Format("~/clienthistory.aspx?id={0}&m={1}", NewClientID, "liClient"));
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }
}